// pages/home/goodsDetail/index.js
const {
  baseURL, ip
} = require('../../../fetch/http')
const api = require('../../../fetch/api')
const { goods } = require('../../../fetch/api')
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseURL: baseURL,
    ip: ip,
    index: 0, // 轮播图片下标
    selectedIndex: 0, 
    show: false,
    goods: null,
    spec: []
  },
  async confirm() {
    let {
      goods,
      spec,
      selectedIndex,
      num
    } = this.data

    wx.navigateTo({
      url: `/pages/home/submit/index?id=${spec[selectedIndex].id}`,
    })
  },
  swiperChange(e) {
    let {
      current
    } = e.detail
    this.setData({
      index: current
    })
  },
  choose(e) {
    const {
      index
    } = app.dataSet(e)
    this.setData({
      selectedIndex: index
    })
  },
  close() {
    this.setData({
      show: false,
    })
  },
  open() {
    // 正常商品有规格
    if(this.data.spec.length){
      app.authTo(()=>{
        this.setData({
          show: true
        })
      })
    }else{
      // 秒杀 拼团 一元购
      wx.navigateTo({
        url: `/pages/home/submit/index?goodId=${this.data.goods.id}`,
      })
    }
 
  },
  async goodsDetail(){
    let res = await api.goodsDetail({
      goods_id: this.data.goods_id
    })

      this.setData({
        goods: res.data,
        spec: res.data.spec
      })

   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.userId){
      wx.setStorageSync('recommend', options.userId)
    }
    this.setData({
      goods_id: options.id
    })
  
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.goodsDetail()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    let userId = wx.getStorageSync('id')
    return {
      // imageUrl: this.data.ip + '/static/wx/img/share.png',
      title: this.data.goods.goodsName,
      path: `/pages/home/goodsDetail/index?userId=${userId}&id=${this.data.goods.id}`
    }
  }
})